//
//  PlayView.m
//  27-Block练习-播放按钮
//
//  Created by dllo on 16/8/19.
//  Copyright © 2016年 ZHW. All rights reserved.
//

#import "PlayView.h"
#import "UIView+ChangeFrame.h"
@interface PlayView ()
@property (nonatomic, strong) UIButton *playButton;
@property (nonatomic, strong) UIButton *lastButton;
@property (nonatomic, strong) UIButton *nextButton;
@property (nonatomic, copy) void (^play)(UIButton * play);
@property (nonatomic, copy) void (^pause)(UIButton *pause);
@property (nonatomic, copy) void (^last)(UIButton *last);
@property (nonatomic, copy) void (^next)(UIButton *next);
@end

@implementation PlayView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setUp];
    }
    return self;
}
- (void)setUp {
    _playButton = [self buttonImageWithName:@"player_btn_play_normal" action:@selector(playButtonClicked:)];
    _lastButton = [self buttonImageWithName:@"player_btn_pre_normal" action:@selector(lastButtonClicked:)];
    _nextButton = [self buttonImageWithName:@"player_btn_next_normal" action:@selector(nextButtonClicked:)];
}

- (UIButton *)buttonImageWithName:(NSString *)name action:(SEL)action {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
    [self addSubview:button];
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _playButton.frame = CGRectMake(0, 0, self.frame.size.width * 0.2, self.frame.size.height);
    _playButton.center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
    
    _lastButton.z_width = 60;
    _lastButton.z_height = 60;
    _lastButton.z_centerY = _playButton.z_centerY;
    _lastButton.z_centerX = _playButton.z_centerX - (_playButton.z_width / 2 + _lastButton.z_width / 2 + 30);
    
    _nextButton.z_width = _lastButton.z_width;
    _nextButton.z_height = _lastButton.z_height;
    _nextButton.z_centerY = _lastButton.z_centerY;
    _nextButton.z_centerX = _playButton.z_centerX + (_playButton.z_width / 2 + _nextButton.z_width / 2 + 30);
}

- (void)play:(void (^)(UIButton *))play pause:(void (^)(UIButton *))pause {
    self.play = play;
    self.pause = pause;
}

- (void)playButtonClicked:(UIButton *)button {
    [_playButton setImage:[UIImage imageNamed:@"player_btn_pause_normal"] forState:UIControlStateNormal];
    [_playButton removeTarget:self action:@selector(playButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [_playButton addTarget:self action:@selector(pauseButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.play(button);
}

- (void)pauseButtonClicked:(UIButton *)button {
    [_playButton setImage:[UIImage imageNamed:@"player_btn_play_normal"] forState:UIControlStateNormal];
    [_playButton removeTarget:self action:@selector(pauseButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [_playButton addTarget:self action:@selector(playButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.pause(button);
}

- (void)last:(void (^)(UIButton *))last {
    self.last = last;
}

- (void)next:(void (^)(UIButton *))next {
    self.next = next;
}

- (void)lastButtonClicked:(UIButton *)button {
    self.last(button);
}

- (void)nextButtonClicked:(UIButton *)button {
    self.next(button);
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
